package cn.dnui_zjq823.dnuifood_16110100823.fragment;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import cn.dnui_zjq823.dnuifood_16110100823.R;
import cn.dnui_zjq823.dnuifood_16110100823.bean.UserBean;
import cn.dnui_zjq823.dnuifood_16110100823.controller.RegisterActivity;
import cn.dnui_zjq823.dnuifood_16110100823.controller.UpdateActivity;
import cn.dnui_zjq823.dnuifood_16110100823.listener.onRetrofitListener;
import cn.dnui_zjq823.dnuifood_16110100823.model.UserModel;

import static android.content.Context.MODE_PRIVATE;

/**
 * Created by dell on 2018/12/6.
 */

public class MyFragment extends BaseFragment implements onRetrofitListener {
    private TextView my_username,my_mobile,my_address,my_comment;
    private Button btn_my;
    SharedPreferences sp;
    private int user_id;
    private UserBean bean;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_my, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        UserModel model=new UserModel();
        sp=this.getActivity().getSharedPreferences("login",MODE_PRIVATE);
        String id=sp.getString("userid",null);
        user_id=Integer.parseInt(id);
        model.getUserById(user_id,this);
        initview(view);
    }


    @Override
    public void onSuccess(Object object, int flag) {
        if (flag==4) {
            bean = (UserBean) object;
            my_username.setText("用户名："+bean.getUsername());
            my_mobile.setText("电话号码："+bean.getMobilenum());
            my_address.setText("地址："+bean.getAddress());
            my_comment.setText("备注说明："+bean.getComment());
        }
    }

    @Override
    public void onFalure(String msg) {
        Toast.makeText(context, "失败："+msg, Toast.LENGTH_SHORT).show();
    }

    private void initview(View view){
        my_username=view.findViewById(R.id.my_username);
        my_mobile=view.findViewById(R.id.my_mobile);
        my_address=view.findViewById(R.id.my_address);
        my_comment=view.findViewById(R.id.my_comment);
        btn_my=view.findViewById(R.id.btn_my);
        btn_my.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(context,UpdateActivity.class);
                startActivity(intent);
                int int1=bean.getUser_id();
                String str1=String.valueOf(bean.getUsername());
                String str2=String.valueOf(bean.getUserpass());
                String str3=String.valueOf(bean.getMobilenum());
                String str4=String.valueOf(bean.getAddress());
                intent.putExtra("user_id",int1);
                intent.putExtra("username",str1);
                intent.putExtra("userpass",str2);
                intent.putExtra("mobile",str3);
                intent.putExtra("address",str4);
                context.startActivity(intent);
            }
        });
    }
}
